package controllers;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import commands.Commands;
import commands.Dispatcher;
import commands.LoadEntryCommand;
import commands.NewEntryCommand;
import commands.RemoveEntryCommand;

import core.Schedule;

public class ScheduleController implements CommandListener{
	private Schedule m_schedule;
	private static ScheduleController m_instance;
	
	public static ScheduleController getInstance(){
		if(m_instance == null){
			m_instance = new ScheduleController();
		}
		return m_instance;
	}
	
	public Schedule getSchedule(){
		return m_schedule;
	}
	
	private ScheduleController(){
		m_schedule = new Schedule();
		Dispatcher.getInstance().registerListener(this, Commands.NewEntry);
		Dispatcher.getInstance().registerListener(this, Commands.LoadEntry);
		Dispatcher.getInstance().registerListener(this, Commands.RemoveEntry);
	}

	public void commandAction(Command cmd, Displayable arg1) {
		switch(cmd.getCommandType()){
		case Command.ITEM:
			if (cmd.getLabel().equals(Commands.NewEntry)){
				NewEntryCommand neCmd = (NewEntryCommand)cmd;
				m_schedule.addEntry(neCmd.getEntry());
				Dispatcher.getInstance().handleCommand(new Command(Commands.Updated, Command.ITEM, 1));
			}
			if (cmd.getLabel().equals(Commands.LoadEntry)){
				LoadEntryCommand leCmd = (LoadEntryCommand)cmd;
				m_schedule.addEntry(leCmd.getEntry());
				Dispatcher.getInstance().handleCommand(new Command(Commands.Updated, Command.ITEM, 1));
			}
			if (cmd.getLabel().equals(Commands.RemoveEntry)){
				RemoveEntryCommand reCmd = (RemoveEntryCommand)cmd;
				m_schedule.deleteEntry(reCmd.getEntry());
				Dispatcher.getInstance().handleCommand(new Command(Commands.Updated, Command.ITEM, 1));
			}
		}
	}
	
	
}
